﻿// Copyright (c) Vitaly Kamiansky. All rights reserved. 
// See License.txt in the project root.

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;

namespace Property.Objects
{
    public class CheckInputProperty<T> : InputProperty<T>, IValidatable, IDataErrorInfo
    {
        protected CheckInputProperty(IReloaderPrototype<T> reloaderPrototype, Func<T, string> check, T defaultValue)
            : base(reloaderPrototype, defaultValue)
        {
            if (check == null) throw new ArgumentNullException("check");

            _Check = check;
        }

        //=//

        protected override void ApplyValue(T value)
        {
            base.ApplyValue(value);
            Validate();
        }

        public void Validate()
        {
            ErrorState = _Check(Input);
            OnPropertyChanged("Error");
        }

        private readonly Func<T, string> _Check;

        private string ErrorState;

        public string Error { get { return ErrorState; } }

        public string this[string columnName] { get { return _Check(Input); } }
    }
}
